﻿using System;
using org.se.fs.presenter.ITask.Profile;
using org.se.fs.presenter.IView.Profile;
using org.se.fs.presenter.Library;

namespace org.se.fs.presenter.View.Profile
{
    public class BranchSubMenu : Template.SubMenu
    {
        private readonly IBranchSubMenu _view;
        private readonly IBranch _task;

        public BranchSubMenu(IBranchSubMenu view) : this(view, new Task.Context.Branch()) { }

        public BranchSubMenu(IBranchSubMenu view, IBranch task)
        {
            _view = view;
            _task = task;
        }

        public override void Initialize()
        {
            _task.OnDelete += TaskOnInsert;
            _task.OnInsert += TaskOnInsert;
            _task.OnUpdate += TaskOnUpdate;

            _view.SetLabel(Data.Rows.Count.Equals(0) ? @"Create New Branch" : @"Modify Branch");
            _view.ButtonAddVisible(Data.Rows.Count.Equals(0));
            _view.ButtonUpdateVisible(!Data.Rows.Count.Equals(0));

            if (!Data.Rows.Count.Equals(0))
                SetData();
        }

        protected override void TaskOnInsert(object source, FormEvent e)
        {
            _view.ButtonAddEnable(true);
            TriggerModify(new FormEvent(e.IsSuccess) { Exception = e.Exception, Value = e.Value });
        }

        protected override void TaskOnUpdate(object source, FormEvent e)
        {
            _view.ButtonUpdateEnable(true);
            TriggerModify(new FormEvent(e.IsSuccess) {Exception = e.Exception, Value = e.Value});
        }

        protected override void SetData()
        {
            if(Data.Rows.Count.Equals(0))
                return;

            _view.FullName = Data.Rows[0]["name"].ToString();
            _view.Code = Data.Rows[0]["code"].ToString();
            _view.Address = Data.Rows[0]["address"].ToString();
            _view.Fax = Data.Rows[0]["fax_num"].ToString();
            _view.Email = Data.Rows[0]["email"].ToString();
            _view.Mobile = Data.Rows[0]["mobile_num"].ToString();
            _view.Tel = Data.Rows[0]["tel_num"].ToString();
        }

        public override void Add()
        {
            if (!Validate())
                return;

            _view.ButtonAddEnable(false);
            var a = Data.NewRow();
            a["name"] = _view.FullName;
            a["code"] = _view.Code;
            a["address"] = _view.Address;
            a["email"] = _view.Email;
            a["fax_num"] = _view.Fax;
            a["mobile_num"] = _view.Mobile;
            a["tel_num"] = _view.Tel;
            a["user_create_id"] = Convert.ToUInt64(Config.User.Rows[0]["id"].ToString());
            a["last_name"] = Config.User.Rows[0]["last_name"].ToString();
            a["first_name"] = Config.User.Rows[0]["first_name"].ToString();
            a["middle_name"] = Config.User.Rows[0]["middle_name"].ToString();
            a["user_name"] = Config.User.Rows[0]["user_name"].ToString();
            Data.Rows.Add(a);
            _task.Insert(Data);
        }

        public override void Modify()
        {
            if(!Validate())
                return;

            _view.ButtonUpdateEnable(false);
            Data.Rows[0]["name"] = _view.FullName;
            Data.Rows[0]["code"] = _view.Code;
            Data.Rows[0]["address"] = _view.Address;
            Data.Rows[0]["email"] = _view.Email;
            Data.Rows[0]["fax_num"] = _view.Fax;
            Data.Rows[0]["mobile_num"] = _view.Mobile;
            Data.Rows[0]["tel_num"] = _view.Tel;
            Data.Rows[0]["user_create_id"] = Convert.ToUInt64(Config.User.Rows[0]["id"].ToString());
            Data.Rows[0]["last_name"] = Config.User.Rows[0]["last_name"].ToString();
            Data.Rows[0]["first_name"] = Config.User.Rows[0]["first_name"].ToString();
            Data.Rows[0]["middle_name"] = Config.User.Rows[0]["middle_name"].ToString();
            Data.Rows[0]["user_name"] = Config.User.Rows[0]["user_name"].ToString();
            _task.Update(Data);
        }

        protected override bool Validate()
        {
            if (string.IsNullOrEmpty(_view.FullName.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty name.") });
                _view.FocusName();
                return false;
            }

            if (string.IsNullOrEmpty(_view.Code.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty code.") });
                _view.FocusCode();
                return false;
            }

            if (string.IsNullOrEmpty(_view.Address.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty address.") });
                _view.FocusAddress();
                return false;
            }

            TriggerValidate(new FormEvent(true));
            return true;
        }
    }
}
